package com.example.servicelearn

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private val TAG = MainActivity::class.java.name

    private val mGetReplyMessenger: Messenger = Messenger(AMessageHandler())
    private var mService: Messenger? = null

    open inner class AMessageHandler : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                Constants.MSG_FROM_SERVICE -> {
                    Log.d(
                        TAG,
                        "received msg form service: msg = [" + msg.data.getString(Constants.MSG_KEY)
                            .toString() + "]"
                    )
                    Toast.makeText(
                        this@MainActivity,
                        "received msg form service: msg = [" + msg.data
                            .getString(Constants.MSG_KEY).toString() + "]",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                else -> super.handleMessage(msg)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bindService()

        val tv_main_iod: TextView = findViewById(R.id.tv_main_iod)
        tv_main_iod.setOnClickListener {
            sendMessage()
        }
//        tv_main.seton
    }

    private fun bindService() {

        val mIntent = Intent(this, MessageService::class.java)
        bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE)

        val mIntent1 = Intent(this, AIDLService::class.java)
        startService(mIntent1)


//        val mIntent = Intent(this, MessageService::class.java)
//
//        startService(mIntent)


    }


    private val mServiceConnection: ServiceConnection = object : ServiceConnection {
        /**
         * @param name
         * @param service
         */
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            mService = Messenger(service)
            val msg =
                Message.obtain(null, Constants.MSG_FROM_CLIENT)
            val data = Bundle()
            data.putString(Constants.MSG_KEY, "Hello! This is client.222")
            msg.data = data
            //
            msg.replyTo = mGetReplyMessenger
            try {
                mService?.send(msg)
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }

        /**
         * @param name
         */
        override fun onServiceDisconnected(name: ComponentName) {}
    }

    fun sendMessage() {
        val msg = Message.obtain(null, Constants.MSG_FROM_CLIENT)
        val data = Bundle()
        data.putString(Constants.MSG_KEY, "Hello! This is client.111")
        msg.data = data
        msg.replyTo = mGetReplyMessenger
        try {
            mService!!.send(msg)
        } catch (e: RemoteException) {
            e.printStackTrace()
        }
    }

    override fun onDestroy() {
        unbindService(mServiceConnection)
        super.onDestroy()
    }
}